import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/29 9:58
 * @description BFS
 */
public class Solution3 {
    public static void main(String[] args) {
        Solution3 solution = new Solution3();
        int[] arr = {4,2,3,0,3,1,2};
        solution.canReach(arr, 5);
    }
    public boolean canReach(int[] arr, int start) {
        if (arr[start] == 0) {
            return true;
        }
        int n = arr.length;
        boolean[] visited = new boolean[n];
        Queue<Integer> queue = new LinkedList<>();
        queue.add(start);
        visited[start] = true;
        while (!queue.isEmpty()) {
            int u = queue.poll();
            int lo = u - arr[u], hi = u + arr[u];
            if (lo >= 0 && !visited[lo]) {
                if (arr[lo] == 0) {
                    return true;
                }
                queue.add(lo);
                visited[lo] = true;
            }
            if (hi < n  && !visited[hi]) {
                if (arr[hi] == 0) {
                    return true;
                }
                queue.add(hi);
                visited[hi] = true;
            }
        }
        return false;
    }
}
